package com.example.tellwin.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuwang
 * @Description
 * @email 1558183202@qq.com
 * @date 2017/12/12
 */

public class NetUtil {
    /**
     * 没有连接网络
     */
    public static final int NETWORK_NONE = -1;
    /**
     * 移动网络
     */
    public static final int NETWORK_MOBILE = 0;
    /**
     * 无线网络
     */
    public static final int NETWORK_WIFI = 1;

    public static int getNetWorkStatus(Context context) {
        int netWorkType = NETWORK_NONE;

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            int type = networkInfo.getType();
            System.out.println("=========type=" + type);
            //wifi连接
            if (type == ConnectivityManager.TYPE_WIFI) {
                netWorkType = NETWORK_WIFI;
                //运营商
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                netWorkType = NETWORK_MOBILE;
            }
        }

        return netWorkType;
    }

    static NetUtil instance;
    static ConnectivityManager connectivityManager;

    public List<NetStateListener> listenerList =new ArrayList<>();

    public void registerNetStateListener(NetStateListener listener){
        listenerList.add(listener);
    }

    public void startNetworkMonitor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (connectivityManager != null) {
                connectivityManager.requestNetwork(new NetworkRequest.Builder().build(), new ConnectivityManager.NetworkCallback() {

                    /**
                     * 网络可用的回调
                     */
                    @Override
                    public void onAvailable(Network network) {
                        super.onAvailable(network);
                        Log.e("lzp", "onAvailable");
                        for (int i = 0; i < listenerList.size(); i++) {
                            if(listenerList.get(i)!=null){
                                listenerList.get(i).onAvailable(network);
                            }else {
                                listenerList.remove(i);
                                i--;
                            }
                        }
                    }

                    /**
                     * 网络丢失的回调
                     */
                    @Override
                    public void onLost(Network network) {
                        super.onLost(network);
                        Log.e("lzp", "onLost");
                        for (int i = 0; i < listenerList.size(); i++) {
                            if(listenerList.get(i)!=null){
                                listenerList.get(i).onLost(network);
                            }else {
                                listenerList.remove(i);
                                i--;
                            }
                        }

                  /*  if (phoneCardInfoBean == null) {
                        SharedPreferences sp = getActivity().getSharedPreferences("PhoneCardInfoBean", Context.MODE_PRIVATE);
                        String beanJson = sp.getString("PhoneCardInfoBean", null);
                        phoneCardInfoBean = GsonUtil.jsonToBean(beanJson, PhoneCardInfoBean.class);
                    }
                    if(phoneCardInfoBean==null){
                        return;
                    }
                    //TODO 免费天数要大于0
                    if (phoneCardInfoBean.getIs_valid() == 1 && phoneCardInfoBean.getFree_days() >= 0) {//有效且有免费天数
                        if (verifySimDialog == null) {
                            verifySimDialog = DialogUtils.getVerifySimDialog(getActivity());
                        }
                        //verifySimDialog.getWindow().
                        verifySimDialog.show();
                    }*/
                    }

                    /**
                     * 当建立网络连接时，回调连接的属性
                     */
                    @Override
                    public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
                        super.onLinkPropertiesChanged(network, linkProperties);
                        Log.e("lzp", "onLinkPropertiesChanged");
              /*      if(verifySimDialog!=null&&verifySimDialog.isShowing()){
                        verifySimDialog.dismiss();
                    }*/
                    }

                    /**
                     * 按照官方的字面意思是，当我们的网络的某个能力发生了变化回调，那么也就是说可能会回调多次
                     * <p>
                     * 之后在仔细的研究
                     */
                    @Override
                    public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
                        super.onCapabilitiesChanged(network, networkCapabilities);
                        Log.e("lzp", "onCapabilitiesChanged");
                    }

                    /**
                     * 在网络失去连接的时候回调，但是如果是一个生硬的断开，他可能不回调
                     */
                    @Override
                    public void onLosing(Network network, int maxMsToLive) {
                        super.onLosing(network, maxMsToLive);
                        Log.e("lzp", "onLosing");
                    }

                    /**
                     * 按照官方注释的解释，是指如果在超时时间内都没有找到可用的网络时进行回调
                     */
                    @Override
                    public void onUnavailable() {
                        super.onUnavailable();
                        Log.e("lzp", "onUnavailable");
                    }
                });
            }
        }
    }



    public interface NetStateListener{
        void onAvailable(Network network);
        void onLost(Network network);
    }

}
